package com.hhkj.api.train.controller;

import com.alibaba.fastjson2.JSON;
import com.hhkj.admin.train.common.cont.TrainConst;
import com.hhkj.api.common.session.holder.model.LoginStudent;
import com.hhkj.api.train.common.valid.VSubmit;
import com.hhkj.api.train.model.info.req.TrainStageReq;
import com.hhkj.api.train.model.info.req.TrainSubmitReq;
import com.hhkj.api.train.model.info.res.ApiTrainContinuePractiseRes;
import com.hhkj.api.train.model.info.res.ApiTrainRes;
import com.hhkj.api.train.model.info.res.ApiTrainTestRes;
import com.hhkj.api.train.model.info.res.TrainCorrectRes;
import com.hhkj.api.train.service.biz.ApiTrainBizService;
import com.ruoyi.framework.login.api.ApiLogin;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.exception.GlobalException;
import com.ruoyi.common.utils.secret.AESUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 培训练习Controller
 *
 * @author ruoyi
 * @since 2023-05-24
 */
@RestController
@RequestMapping("/api/train")
public class ApiTrainController extends BaseController
{
    @Autowired
    private ApiTrainBizService apiTrainService;

    /** 根据题库 id 获取测试题库 */
    @ApiLogin(LoginStudent.class)
    @GetMapping("/bank/test/start/{bankId}")
    public AjaxResult startTrainByBankId(@PathVariable("bankId") Long bankId)
    {
        ApiTrainTestRes trainTestRes = apiTrainService.startTrainByBankId(bankId);
        return AjaxResult.successData(AESUtil.encrypt(JSON.toJSONString(trainTestRes)));
    }

    /** 获取练习题库信息 */
    @ApiLogin(LoginStudent.class)
    @GetMapping("/practise/bank")
    public AjaxResult getPractiseBank()
    {
        return AjaxResult.success(apiTrainService.getPractiseBank());
    }

    /** 获取培训详情 */
    @ApiLogin(LoginStudent.class)
    @GetMapping("/{id}")
    public AjaxResult getById(@PathVariable("id") Long id)
    {
        TrainCorrectRes correctTrainRes = apiTrainService.getCorrectResById(id);
        return AjaxResult.successData(AESUtil.encrypt(JSON.toJSONString(correctTrainRes)));
    }

    /** 继续练习 */
    @ApiLogin(LoginStudent.class)
    @GetMapping("/practise/continue")
    public AjaxResult continuePractise()
    {
        ApiTrainContinuePractiseRes lastPractiseTrain = apiTrainService.continuePractiseTrain();
        return AjaxResult.successData(AESUtil.encrypt(JSON.toJSONString(lastPractiseTrain)));
    }

    /** 模拟考试 */
    @ApiLogin(LoginStudent.class)
    @GetMapping("/test/start")
    public AjaxResult getTestTrain()
    {
        ApiTrainTestRes testTrainRes = apiTrainService.startTestTrain();
        return AjaxResult.successData(AESUtil.encrypt(JSON.toJSONString(testTrainRes)));
    }

    /** 提交答案 */
    @ApiLogin(LoginStudent.class)
    @PostMapping("/submit")
    public AjaxResult submit(@Validated(VSubmit.class) @RequestBody TrainSubmitReq req)
    {
        TrainCorrectRes correctRes = apiTrainService.submit(req);
        return AjaxResult.success(correctRes.getId());
    }

    /** 暂存答案 */
    @ApiLogin(LoginStudent.class)
    @PostMapping("/stage")
    public AjaxResult stage(@Validated @RequestBody TrainStageReq req)
    {
        apiTrainService.stage(req);
        return AjaxResult.success();
    }

    /** 暂存后继续练习 */
    @ApiLogin(LoginStudent.class)
    @GetMapping("/stage/practise/continue")
    public AjaxResult stateContinuePractise()
    {
        ApiTrainContinuePractiseRes lastPractiseTrain = apiTrainService.stageContinuePractiseTrain();
        return AjaxResult.successData(AESUtil.encrypt(JSON.toJSONString(lastPractiseTrain)));
    }

    /** 从头开始 */
    @ApiLogin(LoginStudent.class)
    @GetMapping("/practise/start/{modeCode}")
    public AjaxResult getPractiseTrain(@PathVariable("modeCode") String modeCode)
    {
        if (!TrainConst.PracticeMode.getCodeSet().contains(modeCode)) {
            throw new GlobalException("参数错误");
        }
        ApiTrainRes practiseTrain = apiTrainService.startPractiseTrain(modeCode);
        return AjaxResult.successData(AESUtil.encrypt(JSON.toJSONString(practiseTrain)));
    }

    /** 上次培训信息 */
    @ApiLogin(LoginStudent.class)
    @GetMapping("/last/{typeCode}")
    public AjaxResult getLastTrain(@PathVariable("typeCode") String typeCode)
    {
        return AjaxResult.success(apiTrainService.getLastTrain(typeCode));
    }

    /** 获取当前学员课程信息 */
    @ApiLogin(LoginStudent.class)
    @GetMapping("/course")
    public AjaxResult getCourse()
    {
        return AjaxResult.success(apiTrainService.getCourse());
    }
}
